﻿/**
 * @author sminutoli
 * version 1.0
 * 
 * Este control puede contener tanto MovieClips como Imagenes externas
 * resuelve el tema de los resizes, etc.
 * No utiliza más capas, por lo simple que es (consultar con Mariano)
 * Posiblemente valga la pena que contenga un ContentLoader o algo así
 * 
 * version 1.1
 * En realidad el tema de los resizes los resuelve con ayuda de Proportion
 * 
 */
 
import com.dinmax.flash.utilities.math.Proportion;
import com.dinmax.flash.draw.common.Point;
import com.dinmax.flash.draw.control.Control;
import com.dinmax.flash.draw.control.ImageLoader;

class com.dinmax.flash.draw.control.image.ImageContainer
extends Control {
	
	public static var KEEP_WIDTH:Number = 0;
	public static var KEEP_HEIGHT:Number = 1;
	
	private var pathImage:String;
	private var keepRatio:Boolean;
	private var typeRatio:Number=ImageContainer.KEEP_WIDTH;
	private var base:MovieClip;
	private var imageClip:MovieClip;
	
	public function ImageContainer(container:MovieClip, name:String) {
		super(container, name);
		
	};
	
	public function getPathImage():String {
		return pathImage;
	};

	public function setPathImage(pathImage:String):Void {
		this.pathImage = pathImage;
	};
	
	public function getKeepRatio():Boolean {
		return keepRatio;
	};

	public function setKeepRatio(keepRatio:Boolean, type:Number):Void {
		this.keepRatio = keepRatio;
		if(type == ImageContainer.KEEP_HEIGHT){
			this.typeRatio = type;
		};
	};
	
	public function setSize(size:Point):Void {
		super.setSize(size);
		if(base){
			reDraw();
		} else {
				
		};	
	};
	
	public function setLocation(location:Point):Void {
		super.setLocation(location);
		if(base){
			//reDraw();		
		} else {
			
		};	
	};
	
	public function draw():Void {
		removeMovieClip(this.base);
		this.base=this.createMovieClip();
		this.imageClip =ImageLoader.loadImage(base, pathImage);
		this.imageClip._x = this.getLocation().getX();
		this.imageClip._y = this.getLocation().getY();
		this.reDraw();
	};
	
	public function reDraw():Void {
		
		//Creo el movieBase
		if(!this.keepRatio){
			this.imageClip._width = this.getSize().getX();
			this.imageClip._height = this.getSize().getY();
		} else {
			this.reScale(this.imageClip);
		};
		
	};
	
	private function reScale(clip:MovieClip):MovieClip{
		if(this.typeRatio == ImageContainer.KEEP_WIDTH){
			return Proportion.scaleClip(clip, clip._width, this.getSize().getX());
		} else {
			return Proportion.scaleClip(clip, clip._height, this.getSize().getY());
		}
	};

}